<?php
/**
 * @desc 短信统计模型
 * @author quyang
 * @update 2016-11-24
 */
namespace Home\Model;
use Think\Model;
class PackageInfoModel extends BaseModel{
    
    //短信剩余数量表
    protected $tableName = 'tp_package_info';
    
    /**
     * @desc 短信统计模型
     * @author quyang
     * @update 2016-11-24
     */
    public function countsms($where,$id){
        $data = $this->alias('p')
                     ->where($where)
                     ->field('
                         p.f_buy_count AS buyAviliable,
						 p.f_package_count AS smsPackagetotal,
						 p.f_cur_count  as curCount
                         ')->find();
        if($data == false){
            return false;
        }
        //根据userId获取所有充值数量总和
        $arr = D('Home/RechargeMsgLog')->field('SUM(f_msg_num)')->where(array('f_user_id'=>$id))->select();
        $data['buyTotal'] = $arr[0]['SUM(f_msg_num)'];
        if ($data['buyTotal'] == null){
            $data['buyTotal']='0';
        }
        $smstotal = $data['buyTotal']+$data['smsPackagetotal'];
        $packageaviliable = $data['curCount']+$data['buyAviliable'];
        $data['smsPackageAviliable'] = "$packageaviliable";
        $allAviliable = $data['smsPackageAviliable']+$data['buyAviliable'];
        $data['allAviliable'] = "$allAviliable";
        $data['smstotal'] = "$smstotal";
        $data['allAviliable'] = $data['buyAviliable'];
        return $data;
    }
    
    /**
     * @desc 添加短信套餐给用户
     * @param type $uid 用户id
     * @param type $map 数据
     * @author cheng
     */
    public function addSmsNums($uid, $map){
        $info = $this->alias('p')->where(array('p.f_station_id' => $uid))->find();
        if (empty($info)) {
            $id = $this->add($map);
        }
        return $id;  
    }
    
    /**
     * @desc 添加短信套餐给用户
     * @param type $uid 用户id
     * @param type $count 数量
     * @author cheng
     */
    public function saveBuyCount($uid, $count){
        $result = $this->where(array('f_station_id' => $uid))->setInc('f_buy_count',$count); ;
        return $result;  
    }
    

    
    /**
     * @desc 修改短信余额
     * @author quyang
     * @update 2016-12-1
     */
    public function updateSms($station,$smsnum){
        $this->where(array('f_station_id'=>$station))->setDec('f_buy_count',$smsnum);
    }
}